package net.lookyou.boot.demo.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    private final String ROOT_PATH = "http://localhost:4000";

    @Bean
    public Docket docket(Environment environment) {

        List<Parameter> pars = new ArrayList<>();
        // 添加接口请求头参数配置 没有的话 可以忽略
        ParameterBuilder tokenPar = new ParameterBuilder();
        /*tokenPar.name("token")
                .description("jwt")
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(true).build();
        pars.add(tokenPar.build());*/

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo().build())
                .host(ROOT_PATH)
                //是否启动swagger 默认启动
                .enable(true)
                //所在分组
                .groupName(Docket.DEFAULT_GROUP_NAME)
                .select()
                //指定扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("net.lookyou.boot.demo.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }
 
    private ApiInfoBuilder apiInfo() {
        Contact author = new Contact("管进峰", ROOT_PATH, "mr_guanzi@163.com");

        return new ApiInfoBuilder()
                .title("在线api文档")
                //描述字段支持Markdown语法
                .description("```api文档```")//简介
                .version("1.0")//版本
                .termsOfServiceUrl(ROOT_PATH)//服务地址
                .license("")
                .licenseUrl("")
                .contact(author);
 
    }

}